package com.sky.controller.admin;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "新增菜品")
public class DishController {
    @Resource
    private DishService dishService;

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result<String> addDish(@RequestBody DishDTO dishDTO) {
        log.info("[新增菜品] addDish:{}", dishDTO);
        Integer tag = dishService.addDish(dishDTO);
        return Result.success(tag > 0 ? "新增成功" : "新增失败");

    }

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> findByPage(DishPageQueryDTO dishPageQueryDTO) {
        log.info("[新增菜品] dishPageQueryDTO:{}", dishPageQueryDTO);
        PageResult pageResult = dishService.findByPage(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result<String> shanchuById(@RequestParam List<Long> ids) {
        log.info("[删除菜品列表] ids:{}", ids);
        Integer tag = dishService.batchDelete(ids);
        return Result.success(tag > 0 ? "删除成功" : "删除失败");
    }

    @GetMapping("/list")
    @ApiOperation("根据分类Id查询菜品")
    public Result<List<Dish>> List(Long categoryId) {
        log.info("[根据Id查询菜品] categoryId{}", categoryId);
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);


    }

    /**
     * 回显
     *
     * @param id
     * @return
     */

    @GetMapping("/{id}")
    @ApiOperation("回显菜品信息")
    public Result<DishVO> robackLike(@PathVariable Long id) {
        //打印
        log.info("[回显菜品信息] id:{}", id);
        //调用
        DishVO dishVO = dishService.findById(id);
        //响应
        return Result.success(dishVO);
    }

    /**
     * 修改菜品信息
     *
     * @param dishDTO
     * @return
     */

    @PutMapping
    @ApiOperation("修改菜品信息")
    public Result<String> xiuGaiDish(@RequestBody DishDTO dishDTO) {
        //打印
        log.info("[修改菜品信息] dishDTO:{}", dishDTO);
        //调用
        Integer tage = dishService.xiuGaiDish(dishDTO);
        //响应
        return Result.success(tage > 0 ? "修改成功" : "修改失败");
    }

    /**
     * 修改菜品状态
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品状态")
    public Result<String> startOrStopDish(@PathVariable Integer status, Long id) {
        //打印
        log.info("[修改菜品状态] id:{}", id);
        //调用
        Integer tage = dishService.startOrStopDish(status, id);
        //响应
        return Result.success(tage > 0 ? "启用成功" : "启用失败");
    }
}
